package com.weatherquery.servlet;

import com.weatherquery.informbean.WeatherData;
import com.weatherquery.service.WeatherService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author Natural-Pride
 * @ProjectName WeatherQuery
 * @Date 2023/12/19 16:55
 * @Explain
 */
@WebServlet("/WeatherSearchServlet")
public class WeatherSearchServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String cityName = request.getParameter("cityName");
        WeatherService weatherSearchList = new WeatherService();

        List<WeatherData> weatherList=weatherSearchList.searchWeather(cityName);
        request.setAttribute("weatherList", weatherList);
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }
}
